package com.lagou.service.impI;

import com.lagou.base.StatusCode;
import com.lagou.dao.CourseDao;
import com.lagou.dao.impI.CourseDaoImpl;
import com.lagou.pojo.Course;
import com.lagou.service.CourseService;
import com.lagou.utils.DateUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author wsl
 * @Date 2021/10/7 21:50
 * @Project_Name maven
 *
 * 课程管理模块，Service层 实现类
 */
public class CourseServiceImpI implements CourseService {

    //创建CourseDao对象
    CourseDao courseDao = new CourseDaoImpl();

    //1.创建查询课程列表
    @Override
    public List<Course> findCourseList() {

        List<Course> courseList = courseDao.findCourseList();
        return courseList;
    }

    //2.根据条件查询课程信息
    @Override
    public List<Course> findByCourseNameAndStatus(String courseName, String status) {
        List<Course> courseList = courseDao.findByCourseNameAndStatus(courseName, status);
        return courseList;
    }

    //3.保存课程营销信息
    @Override
    public String saveCourseSalesInfo(Course course) {
        //1.补全课程营销信息
        String strDate = DateUtils.getDateFormart();
        course.setCreate_time(strDate);
        course.setUpdate_time(strDate);
        course.setStatus(1);

        //2.执行插入操作
        int row = courseDao.saveCourseSalesInfo(course);

        //3.判断是否插入成功
        if (row > 0){
            String jsonResult = StatusCode.SUCCESS.toString();
            return jsonResult;
        } else {
            String jsonResult = StatusCode.FAIL.toString();
            return jsonResult;
        }

    }

    //4.根据课程ID查询课程信息
    @Override
    public Course findCourseById(int id) {

        return courseDao.findCourseById(id);

    }

    //5.修改课程营销信息
    @Override
    public String updateCourseSalesInfo(Course course) {

        int row = courseDao.updateCourseSalesInfo(course);

        //判断是否保存成功
        if (row > 0){
            //保存成功
            String result = StatusCode.SUCCESS.toString();
            return result;
        } else {
            //保存失败
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }

    //6.修改课程状态
    @Override
    public Map<String, Integer> updateCourseStatus(Course course) {

        //调用dao
        int row = courseDao.updateCourseStatus(course);

        Map<String,Integer> map = new HashMap<>();
        //判断是否修改成功
        if (row > 0){

            if (course.getStatus() == 0){
                map.put("status",0);
            } else {
                map.put("status",1 );
            }

        }
           return map;
    }
}
